/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/
#include <QKeyEvent>
#include "query.h"
#include "checkbox.h"

using namespace SQL;

CheckBox::CheckBox(QWidget *parent)
		: QCheckBox(parent),m_handleFocus(true)
{
	setTristate(false);
	connect(this,SIGNAL(stateChanged( int )),SLOT(bindValue(int)));
}

bool CheckBox::handleFocus()
{
	return m_handleFocus;
}

void CheckBox::setHandleFocus(bool handleFocus)
{
	m_handleFocus=handleFocus;
}

const QString & CheckBox::queryName()
{
	return m_queryName;
}

void CheckBox::setQueryName(const QString & queryName)
{
	m_queryName = queryName;

	Query *q = defaultDatabase()->query(m_queryName);

	if (!q)
		return;
	connect(q, SIGNAL(recordChanged(const QSqlRecord&)), SLOT(recordChanged(const QSqlRecord&)));
	connect(q, SIGNAL(beforeInsert()), SLOT(setReadWrite()));
	connect(q, SIGNAL(beforeUpdate()), SLOT(setReadWrite()));
	connect(q, SIGNAL(afterSelect()), SLOT(setReadOnly()));
	connect(q, SIGNAL(afterClose()), SLOT(setReadOnly()));
	connect(q, SIGNAL(afterPost()), SLOT(setReadOnly()));
	connect(q, SIGNAL(afterCancel()), SLOT(setReadOnly()));
	setDisabled(q->state() == Query::Select);
}

const QString & CheckBox::fieldName()
{
	return m_fieldName;
}

void CheckBox::setFieldName(const QString & fieldName)
{
	m_fieldName = fieldName;
	if (defaultDatabase()->query(m_queryName))
		setCheckState(defaultDatabase()->query(m_queryName)->value(m_fieldName).toBool() ? Qt::Checked : Qt::Unchecked);
}

void CheckBox::recordChanged(const QSqlRecord& record)
{
	setCheckState(record.value(m_fieldName).toBool() ? Qt::Checked : Qt::Unchecked);
}

void CheckBox::bindValue(int state)
{
	Query *q = defaultDatabase()->query(m_queryName);
	if (!q || !m_fieldName.length())
		return;
	q->setValue(m_fieldName, state == Qt::Checked);
}


void CheckBox::keyPressEvent(QKeyEvent * event)
{
	if (event->key() == Qt::Key_Escape)
	{
		if (m_handleFocus)
			 focusPreviousChild();
		emit(escapePressed());
	}
	if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter)
	{
		bindValue(checkState());
		if (m_handleFocus)
			 focusNextChild();
		emit(returnPressed());
	}
	QCheckBox::keyPressEvent(event);
}

void CheckBox::setReadWrite()
{
	setFocusPolicy( Qt::StrongFocus );
	setEnabled(true);
	Query *q = defaultDatabase()->query(m_queryName);
	if (!q || !m_fieldName.length())
		return;
	if (q->state()==Query::Insert)
		setCheckState(Qt::Unchecked);
	q->setValue(m_fieldName, checkState() == Qt::Checked);
}

void CheckBox::setReadOnly()
{
	setDisabled(true);
	setFocusPolicy( Qt::NoFocus );
	Query *q = defaultDatabase()->query(m_queryName);
	if (!q || !m_fieldName.length())
		return;
	if (q->state()==Query::Select)
		setCheckState(q->value(m_fieldName).toBool() ? Qt::Checked : Qt::Unchecked);
}

